package guis.member;

import exceptions.RepositoryException;
import guis.RegisterPanel;
import guis.TablePanel;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;

import classes.Gender;
import classes.Iterator;
import classes.Member;

import communication.Facade;

public class MemberPanel extends RegisterPanel< Member >
{
	private static final long		serialVersionUID	= -2238677678739841723L;

	public MemberPanel( JFrame parent, Facade facade )
	{
		super( parent, facade );
		this.createTables();
		this.createActions();
	}

	private void createTables()
	{
		this.tablePanel = new TablePanel< Member >( 10, 10, 620, 530 );
		formPanel.add( tablePanel );
		tablePanel.setHeader( new Member( null, Gender.male, null, 0, null, null, false, 0 ) );
		tablePanel.hideCollumn( 4 );
		tablePanel.hideCollumn( 4 );
		tablePanel.hideCollumn( 4 );
		tablePanel.hideCollumn( 1 );
		loadTable( null );
	}
	
	private void createActions()
	{
		insertButton.addActionListener( new ActionListener()
		{
			
			@Override
			public void actionPerformed( ActionEvent arg0 )
			{
				new InsertMemberWindow( parent, self, facade );
			}

		} );
		
		editButton.addActionListener( new ActionListener()
		{

			@Override
			public void actionPerformed( ActionEvent arg0 )
			{
				new EditMemberWindow( parent, self, facade );
			}

		} );
		
		findButton.addActionListener( new ActionListener()
		{

			@Override
			public void actionPerformed( ActionEvent arg0 )
			{
				new FindMemberWindow( parent, self, facade );
			}

		} );

		removeButton.addActionListener( new ActionListener()
		{

			@Override
			public void actionPerformed( ActionEvent arg0 )
			{
				Object[] options = { "      Sim      ", "      Não      " };
				int result = JOptionPane.showOptionDialog( null, "Deseja deletar o membro selecionado?", "Deleção", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[1] );				
				
				if( result == JOptionPane.YES_OPTION )
				{
					try
					{
						JTable table = tablePanel.getTable();
						int selectedIndex = table.getSelectedRow();
						facade.removeMember( Integer.parseInt( ( String ) table.getModel().getValueAt( selectedIndex, 0 ) ) );
						tablePanel.remove();
					}
					catch ( NumberFormatException e )
					{
						e.printStackTrace();
					}
					catch ( RepositoryException e )
					{
						e.printStackTrace();
					}
				}
			}

		} );

		tablePanel.getTable().getSelectionModel().addListSelectionListener( new ListSelectionListener()
		{

			@Override
			public void valueChanged( ListSelectionEvent arg0 )
			{
				JTable table = tablePanel.getTable();
				if ( table.getSelectionModel().getMaxSelectionIndex() >= 0 && table.getRowCount() > 0 )
				{
					editButton.setEnabled( true );
					removeButton.setEnabled( true );
				}
				else
				{
					editButton.setEnabled( false );
					removeButton.setEnabled( false );
				}
			}

		} );

	}

	public void loadTable( Iterator< Member > iterator )
	{
		try
		{
			DefaultTableModel dtm = ( DefaultTableModel ) tablePanel.getTable().getModel();
			while( dtm.getRowCount() > 0 )
			{
				dtm.removeRow( 0 );
			}
			
			if( iterator == null ) iterator = facade.getIteratorMember();
			while( iterator.hasNext() )
			{
				tablePanel.addRow( iterator.next() );
			}
		}
		catch ( RepositoryException e ){}
	}
}